{% extends "base.html" %}

{% block content %}
<div class="row">
    <div class="col-md-12">
        <h2>优秀学生推优</h2>
        
        {% if current_user.role == 'student' %}
            <div class="card mt-4">
                <div class="card-header">
                    <h4>提交推优申请</h4>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ url_for('submit_outstanding') }}" enctype="multipart/form-data">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        
                        <!-- 基本信息 -->
                        <div class="row">
                            <div class="col-md-4 mb-3">
                                <label class="form-label">姓名</label>
                                <input type="text" class="form-control" value="{{ current_user.real_name }}" readonly>
                            </div>
                            <div class="col-md-4 mb-3">
                                <label class="form-label">学号</label>
                                <input type="text" class="form-control" value="{{ current_user.username }}" readonly>
                            </div>
                            <div class="col-md-4 mb-3">
                                <label class="form-label">班级</label>
                                <input type="text" class="form-control" value="{{ current_user.class_name }}" readonly>
                            </div>
                        </div>

                        <!-- 申请信息 -->
                        <div class="mb-3">
                            <label class="form-label">申请类型</label>
                            <select class="form-select" name="category" required>
                                <option value="">请选择申请类型</option>
                                <option value="三好学生">三好学生</option>
                                <option value="优秀学生干部">优秀学生干部</option>
                                <option value="优秀团员">优秀团员</option>
                                <option value="优秀团干部">优秀团干部</option>
                            </select>
                        </div>

                        <div class="mb-3">
                            <label class="form-label">学期</label>
                            <select class="form-select" name="semester" required>
                                <option value="">请选择学期</option>
                                <option value="2023-2024">2023-2024</option>
                                <option value="2024-2025">2024-2025</option>
                            </select>
                        </div>

                        <!-- 综合评定成绩 -->
                        <div class="mb-3">
                            <label class="form-label">综合评定成绩</label>
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>学业成绩</th>
                                        <th>德育素质</th>
                                        <th>体育素质</th>
                                        <th>能力素质</th>
                                        <th>劳育素质</th>
                                        <th>总分</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr>
                                        {% if evaluation %}
                                            <td>{{ evaluation.academic_score }}</td>
                                            <td>{{ evaluation.moral_score }}</td>
                                            <td>{{ evaluation.physical_score }}</td>
                                            <td>{{ evaluation.ability_score }}</td>
                                            <td>{{ evaluation.labor_score }}</td>
                                            <td>{{ evaluation.total_score }}</td>
                                        {% else %}
                                            <td colspan="6" class="text-center">暂无综合评定成绩</td>
                                        {% endif %}
                                    </tr>
                                </tbody>
                            </table>
                        </div>

                        <!-- 申请理由 -->
                        <div class="mb-3">
                            <label class="form-label">申请理由</label>
                            <textarea class="form-control" name="reason" rows="5" required></textarea>
                        </div>

                        <!-- 证明材料 -->
                        <div class="mb-3">
                            <label class="form-label">证明材料（选填）</label>
                            <input type="file" class="form-control" name="proof_file" accept=".pdf,.jpg,.jpeg,.png">
                            <div class="form-text">请上传相关证明材料（如获奖证书等），支持PDF、JPG、PNG格式，大小不超过5MB</div>
                        </div>

                        <button type="submit" class="btn btn-primary">提交申请</button>
                    </form>
                </div>
            </div>

            <!-- 显示历史申请记录 -->
            <div class="card mt-4">
                <div class="card-header">
                    <h4>我的申请记录</h4>
                </div>
                <div class="card-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>申请时间</th>
                                <th>申请类型</th>
                                <th>学期</th>
                                <th>状态</th>
                                <th>附件</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in outstandings %}
                            <tr>
                                <td>{{ record.submit_time.strftime('%Y-%m-%d %H:%M') }}</td>
                                <td>{{ record.category }}</td>
                                <td>{{ record.semester }}</td>
                                <td>
                                    {% if record.status == 'pending' %}
                                        <span class="badge bg-warning">待审核</span>
                                    {% elif record.status == 'approved' %}
                                        <span class="badge bg-success">已通过</span>
                                    {% else %}
                                        <span class="badge bg-danger">已驳回</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if record.proof_file %}
                                        <a href="{{ url_for('download_outstanding_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                            查看附件
                                        </a>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        {% elif current_user.role == 'teacher' %}
            <!-- 教师查看界面 -->
            <div class="card mt-4">
                <div class="card-header">
                    <h4>待审核申请</h4>
                </div>
                <div class="card-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学生</th>
                                <th>申请类型</th>
                                <th>学期</th>
                                <th>提交时间</th>
                                <th>状态</th>
                                <th>附件</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in outstandings %}
                            {% if record.status == 'pending' %}
                            <tr>
                                <td>{{ record.student.username }} - {{ record.student.real_name }}</td>
                                <td>{{ record.category }}</td>
                                <td>{{ record.semester }}</td>
                                <td>{{ record.submit_time.strftime('%Y-%m-%d %H:%M') }}</td>
                                <td>
                                    <span class="badge bg-warning">待审核</span>
                                </td>
                                <td>
                                    {% if record.proof_file %}
                                        <a href="{{ url_for('download_outstanding_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                            查看附件
                                        </a>
                                    {% endif %}
                                </td>
                                <td>
                                    <button type="button" class="btn btn-sm btn-success" onclick="approveOutstanding({{ record.id }})">通过</button>
                                    <button type="button" class="btn btn-sm btn-danger" onclick="rejectOutstanding({{ record.id }})">驳回</button>
                                </td>
                            </tr>
                            {% endif %}
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- 已审核申请 -->
            <div class="card mt-4">
                <div class="card-header">
                    <h4>已审核申请</h4>
                </div>
                <div class="card-body">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>学生</th>
                                <th>申请类型</th>
                                <th>学期</th>
                                <th>提交时间</th>
                                <th>审核时间</th>
                                <th>状态</th>
                                <th>附件</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for record in outstandings %}
                            {% if record.status != 'pending' %}
                            <tr>
                                <td>{{ record.student.username }} - {{ record.student.real_name }}</td>
                                <td>{{ record.category }}</td>
                                <td>{{ record.semester }}</td>
                                <td>{{ record.submit_time.strftime('%Y-%m-%d %H:%M') }}</td>
                                <td>{{ record.update_time.strftime('%Y-%m-%d %H:%M') if record.update_time else '' }}</td>
                                <td>
                                    {% if record.status == 'approved' %}
                                        <span class="badge bg-success">已通过</span>
                                    {% else %}
                                        <span class="badge bg-danger">已驳回</span>
                                    {% endif %}
                                </td>
                                <td>
                                    {% if record.proof_file %}
                                        <a href="{{ url_for('download_outstanding_proof', file_id=record.id) }}" class="btn btn-sm btn-secondary">
                                            查看附件
                                        </a>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endif %}
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
function approveOutstanding(recordId) {
    if (confirm('确定要通过这条申请吗？')) {
        const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        
        fetch(`/approve_outstanding/${recordId}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': token
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('审核通过成功！');
                window.location.reload();
            } else {
                alert('操作失败：' + data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('操作失败，请重试');
        });
    }
}

function rejectOutstanding(recordId) {
    if (confirm('确定要驳回这条申请吗？')) {
        const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        
        fetch(`/reject_outstanding/${recordId}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': token
            }
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                alert('驳回成功！');
                window.location.reload();
            } else {
                alert('操作失败：' + data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('操作失败，请重试');
        });
    }
}
</script>
{% endblock %} 